﻿ package{
	//imports
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	//defining the class
	public class Enemy extends MovieClip{
		private var _root:MovieClip;
		public var xSpeed:int;//how fast it's going horizontally
		public var ySpeed:int;//how fast it's going vertically
		public var maxSpeed:int = 3;//how fast it can possibly go
		public var health:int;
		public var level:int;//this will be set to the number passed in
		
		public function Enemy(code:int){
			this.addEventListener(Event.ADDED, beginClass);
			this.addEventListener(Event.ENTER_FRAME, eFrameEvents);
			
			level = code;//set the level to the value passed in for use in other functions
		}
		private function beginClass(e:Event):void{
			_root = MovieClip(root);//defining the root
			
			//enemy specific commands
			
			if(level == 0){ //place holder
				this.graphics.beginFill(0x000000);
				this.graphics.drawCircle(12.5,12.5,5);
				this.graphics.endFill();
				maxSpeed = 0;
				health = 0;
			} else if(level == 1){ //make enemy cyan
				this.graphics.beginFill(0x00FFFF);
				this.graphics.drawCircle(12.5,12.5,5);
				this.graphics.endFill();
				maxSpeed = 4;
				health = 5;
				_root.enemyLimit = 12;
			} else if (level == 2){ // make enemy white
				this.graphics.beginFill(0xFFFFFF);
				this.graphics.drawCircle(12.5,12.5,5);
				this.graphics.endFill();
				maxSpeed = 2;
				health = 15;
				_root.enemyLimit = 48;
			} else if (level == 3){
				this.graphics.beginFill(0xFF00FF);
				this.graphics.drawCircle(12.5,12.5,5);
				this.graphics.endFill();
				maxSpeed = 6;
				health = 1;
				_root.enemyLimit = 6;
			} else {
				this.graphics.beginFill(0xFF0000);
				this.graphics.drawCircle(12.5,12.5,5);
				this.graphics.endFill();
				health = level*5;
			}
			
			//checking what the start direction is
			if(_root.startDir == 'UP'){//if it's starting up
				this.y = 300;//set the y value off the field
				this.x = _root.startCoord;//make the x value where it should be
				this.xSpeed = 0;//make it not move horizontally
				this.ySpeed = -maxSpeed;//make it move upwards
			} else if(_root.startDir == 'RIGHT'){//and so on for other directions
				this.x = -25;
				this.y = _root.startCoord;
				this.xSpeed = maxSpeed;
				this.ySpeed = 0;
			} else if(_root.startDir == 'DOWN'){
				this.y = -25;
				this.x = _root.startCoord;
				this.xSpeed = 0;
				this.ySpeed = maxSpeed;
			} else if(_root.startDir == 'LEFT'){
				this.x = 550;
				this.y = _root.startCoord;
				this.xSpeed = -maxSpeed;
				this.ySpeed = 0;
			}
			
			
		}
		private function eFrameEvents(e:Event):void{
			//move it based on x and y value
			this.x += xSpeed;
			this.y += ySpeed;
			
			//checking what direction it goes when finishing the path
			if(_root.finDir == 'UP'){//if it finishes at the top
				if(this.y <= -25){//if the y value is too high
					destroyThis();//then remove this guy from the field
					_root.lives --;//take away a life
				}
			} else if(_root.finDir == 'RIGHT'){//and so on for other directions
				if(this.x >= 550){
					destroyThis();					
					_root.lives --;
				}
			} else if(_root.finDir == 'DOWN'){
				if(this.y >= 300){
					destroyThis();
					_root.lives --;
				}
			} else if(_root.startDir == 'LEFT'){
				if(this.x <= 0){
					destroyThis();
					_root.lives --;
				}
			}
			
			//remove this from stage when game is over
			if(_root.gameOver){
				destroyThis();
			}
			
			//destroy this if health is equal to or below 0
			if(health <= 0){
				destroyThis();
				_root.money += 5*level;
			}
		}
		public function destroyThis():void{
			//this function will make it easier to remove this from stage
			this.removeEventListener(Event.ENTER_FRAME, eFrameEvents);
			this.parent.removeChild(this);
			_root.enemiesLeft --;
		}
	}
}